/*
 * Copyright (C) 2013 National University of Defense Technology(NUDT) & Kylin Ltd.
 *
 * Authors:
 *  Kobe Lee   kobe24_lixiang@126.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "receive_dbus.h"
#include <QDebug>

DBusGConnection *connection = NULL;
GError *error = NULL;
DBusGProxy *proxy = NULL;

//dbus初始化
int dbus_init()
{
    if((connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error)) == NULL) {
    error = NULL;
        return 0;
    }
//    DBusGProxy *proxy = NULL;
    if((proxy = dbus_g_proxy_new_for_name(connection, "org.freedesktop.activation", "/org/freedesktop/activation", "org.freedesktop.activation.interface")) == NULL) {
        return 0;
    }
    return 1;
}

//获取dbus后台数据函数
QStringList* byte6_get_init_data()
{
//    DBusGConnection *connection = NULL;
//    GError *error = NULL;
    int ret = 0;
    QStringList *rtnlist = new QStringList();
    QString serial = "", registration = "", qrcode = "";
    QString serial_num = "",registration_num = "",qrcode_num = "";
    gchar *request_name_str = NULL;
    guint request_name_int = 0;


    ret = dbus_g_proxy_call(proxy, "serial_number", &error,
                G_TYPE_INVALID,
                G_TYPE_STRING, &request_name_str,
                G_TYPE_INT, &request_name_int,
                G_TYPE_INVALID);
    error = NULL;
    printf("serial_number->%s\n", request_name_str);
    printf("serial_number_i->%d\n", request_name_int);
    serial = request_name_str;
    serial_num =  QString::number(request_name_int);

    ret = dbus_g_proxy_call(proxy, "register_number", &error,
                G_TYPE_INVALID,
                G_TYPE_STRING, &request_name_str,
                G_TYPE_INT, &request_name_int,
                G_TYPE_INVALID);
    printf("register_number->%s\n", request_name_str);
    printf("register_number_i->%d\n", request_name_int);
    registration = request_name_str;
    registration_num =  QString::number(request_name_int);
    error = NULL;

    ret = dbus_g_proxy_call(proxy, "qrcode", &error,
                G_TYPE_INVALID,
                G_TYPE_STRING, &request_name_str,
                G_TYPE_INT, &request_name_int,
                G_TYPE_INVALID);
    printf("qrcode->%s\n", request_name_str);
    printf("qrcode_i->%d\n", request_name_int);
    qrcode = request_name_str;
    qrcode_num =   QString::number(request_name_int);


	if(!ret) {
		if(error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
            		printf("kobe: Caught remote method exception %s:%s",dbus_g_error_get_name(error),error->message);
        	else
            		printf("Error : %s\n", error->message);
        	g_error_free(error); 
		error = NULL;
	}

    rtnlist->append(serial);
    rtnlist->append(serial_num);
    rtnlist->append(registration);
    rtnlist->append(registration_num);
    rtnlist->append(qrcode);
    rtnlist->append(qrcode_num);
    return rtnlist;
}

QStringList* byte8_get_init_data()
{
//    DBusGConnection *connection = NULL;
//    GError *error = NULL;
    int ret = 0;
    QStringList *rtnlist = new QStringList();
    QString serial = "", registration = "", qrcode = "";
    QString serial_num = "",registration_num = "",qrcode_num = "";
    gchar *request_name_str = NULL, *char_serial;
    guint request_name_int = 0;

    ret = dbus_g_proxy_call(proxy, "serial_number", &error,
                G_TYPE_INVALID,
                G_TYPE_STRING, &request_name_str,
                G_TYPE_INT, &request_name_int,
                G_TYPE_INVALID);
    error = NULL;
    printf("serial_number->%s\n", request_name_str);
    printf("serial_number_i->%d\n", request_name_int);
    char_serial = request_name_str;
    serial = request_name_str;
    serial_num =  QString::number(request_name_int);

//    if( serial.length() != 7 && serial_num == "0")
    if(serial_num == "0")
    {
        ret = dbus_g_proxy_call(proxy, "new_register_number", &error,
                    G_TYPE_STRING, char_serial,
                    G_TYPE_INVALID,
                    G_TYPE_STRING, &request_name_str,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("new_register_number->%s\n", request_name_str);
        printf("new_register_number_i->%d\n", request_name_int);
        registration = request_name_str;
        registration_num =  QString::number(request_name_int);
        error = NULL;

        ret = dbus_g_proxy_call(proxy, "new_qrcode", &error,
                    G_TYPE_STRING, char_serial,
                    G_TYPE_INVALID,
                    G_TYPE_STRING, &request_name_str,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("new_qrcode->%s\n", request_name_str);
        printf("new_qrcode_i->%d\n", request_name_int);
        qrcode = request_name_str;
        qrcode_num =   QString::number(request_name_int);
    }

    if(!ret) {
        if(error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
                    printf("kobe: Caught remote method exception %s:%s",dbus_g_error_get_name(error),error->message);
            else
                    printf("Error : %s\n", error->message);
            g_error_free(error);
        error = NULL;
    }

    rtnlist->append(serial);
    rtnlist->append(serial_num);
    rtnlist->append(registration);
    rtnlist->append(registration_num);
    rtnlist->append(qrcode);
    rtnlist->append(qrcode_num);
    return rtnlist;
}

//获取序列号
QString get_serial_number()
{
    int ret = 0;
    guint request_name_int =0;
    gchar *request_name_str = NULL;
    if (proxy != NULL) {


        ret = dbus_g_proxy_call(proxy, "serial_number", &error,
                    G_TYPE_INVALID,
                    G_TYPE_STRING, &request_name_str,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        error = NULL;
        printf("serial_number->%s\n", request_name_str);
        printf("serial_number_i->%d\n", request_name_int);

    }
    return request_name_str;
}

//获取激活时间
QString status_date()
{
    int ret = 0;
    guint request_name_int =0;
    gchar *request_name_str = NULL;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "date", &error,
                    G_TYPE_INVALID,
//                    G_TYPE_INT, &request_name_result,
                    G_TYPE_STRING, &request_name_str,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("date->%s\n", request_name_str);
        printf("date_i->%d\n",request_name_int);
        error = NULL;
    }
    return request_name_str;
}

//获取过期时间
QString expired_date()
{
    int ret = 0;
    guint request_name_int =0;
    gchar *request_name_str = NULL;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "expired", &error,
                    G_TYPE_INVALID,
//                    G_TYPE_INT, &request_name_result,
                    G_TYPE_STRING, &request_name_str,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("expired_date->%s\n", request_name_str);
        printf("expired_date_i->%d\n",request_name_int);
        error = NULL;
    }
    return request_name_str;
}

//获取激活状态
int status_code()
{
    int ret = 0;
    gint request_name_result = 1;
    guint request_name_int =0;
//    gchar *request_name_str = NULL;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "status", &error,
                    G_TYPE_INVALID,
//                    G_TYPE_STRING, &request_name_str,
                    G_TYPE_INT, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("status_value->%d\n", request_name_result);
        printf("status_value_i->%d\n",request_name_int);
        error = NULL;
    }
    return request_name_result;
}

int trial_status()
{
    int ret = 0;
    gint request_name_result = 1;
    guint request_name_int =0;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "trial_status", &error,
                    G_TYPE_INVALID,
                    G_TYPE_INT, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("status_value->%d\n", request_name_result);
        printf("status_value_i->%d\n",request_name_int);
        error = NULL;
    }
    return request_name_result;
}

QString trial_date()
{
    int ret = 0;
    guint request_name_int =0;
    gchar *request_name_str = NULL;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "trial_date", &error,
                    G_TYPE_INVALID,
//                    G_TYPE_INT, &request_name_result,
                    G_TYPE_STRING, &request_name_str,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("expired_date->%s\n", request_name_str);
        printf("expired_date_i->%d\n",request_name_int);
        error = NULL;
    }
    return request_name_str;
}
//获取ukey状态
int find_ukey()
{
    int ret = 1;
    gint ukey_state = 0;
    if (proxy != NULL)
    {
        error = NULL;
        ret = dbus_g_proxy_call(proxy,"find_ukey",&error,
                                G_TYPE_INVALID,
                                G_TYPE_INT, &ukey_state,
                                G_TYPE_INVALID);
        error = NULL;
    }
    return ukey_state;
}
//进行激活
int active_code(gchar *code)
{
    int ret = 0;
    gint request_name_result = 0;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "activate", &error,
                    G_TYPE_STRING, code,
                    G_TYPE_INVALID,
                    G_TYPE_INT, &request_name_result,
                    G_TYPE_INVALID);
        printf("activate->%d\n", request_name_result);
        if (error) {
            printf("DBus exception: %s\n", error->message);
            request_name_result = 100;
            return request_name_result;
        }
    }
    return request_name_result;
}

//退出
int exit_service()
{
    int ret = 0;
    guint request_name_result;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "exit", &error,
                G_TYPE_INVALID,
                G_TYPE_INT, &request_name_result,
                G_TYPE_INVALID);
        error = NULL;
    }
    return ret;
}

//新检测序列号
int can_set_serial()
{
    int ret = 0;
    gint request_name_result = 0;
    guint request_name_int =0;
//    gchar *request_name_str = NULL;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "can_set_serial", &error,
                    G_TYPE_INVALID,
                    G_TYPE_INT, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("can_set_serial->%d\n", request_name_result);
        printf("can_set_serial_i->%d\n",request_name_int);
        error = NULL;
    }
    return request_name_result;
}

//新获取注册码
QStringList* new_register_number(gchar *serial)
{
    int ret = 0;
    QStringList *rtnlist = new QStringList();
    gchar *request_name_result = NULL;
    guint request_name_int =0;
//    gchar *request_name_str = NULL;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "new_register_number", &error,
                    G_TYPE_STRING, serial,
                    G_TYPE_INVALID,
//                    G_TYPE_STRING, &request_name_str,
                    G_TYPE_STRING, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("new_register_number->%s\n", request_name_result);
        printf("new_register_number_i->%d\n",request_name_int);

        error = NULL;
    }
    rtnlist->append(request_name_result);
    rtnlist->append(QString::number(request_name_int));
    return rtnlist;
}


//新获取激活码
QStringList* new_qrcode(gchar *serial)
{
    int ret = 0;
    QStringList *rtnlist = new QStringList();
    gchar *request_name_result = NULL;
    guint request_name_int =0;
//    gchar *request_name_str = NULL;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "new_qrcode", &error,
                    G_TYPE_STRING, serial,
                    G_TYPE_INVALID,
//                    G_TYPE_STRING, &request_name_str,
                    G_TYPE_STRING, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("new_qrcode->%s\n", request_name_result);
        printf("new_qrcode_i->%d\n",request_name_int);

        error = NULL;
    }
    rtnlist->append(request_name_result);
    rtnlist->append(QString::number(request_name_int));
    return rtnlist;
}

//新进行激活
int new_active_code(gchar *serial, gchar *code)
{
    int ret = 0;
    gint request_name_result = 0;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "new_activate", &error,
                    G_TYPE_STRING, serial,
                    G_TYPE_STRING, code,
                    G_TYPE_INVALID,
                    G_TYPE_INT, &request_name_result,
                    G_TYPE_INVALID);
        printf("new_activate->%d\n", request_name_result);
        error = NULL;
    }
    return request_name_result;
}

//获取硬件信息
QString get_encrypted_hardware_info()
{
    int ret = 0;
    gchar *request_name_result = NULL;
    guint request_name_int =0;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "get_encrypted_hardware_info", &error,
                    G_TYPE_INVALID,
                    G_TYPE_STRING, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("get_encrypted_hardware_info->%s\n", request_name_result);
        printf("get_encrypted_hardware_info->%d\n", request_name_int);
        error = NULL;
    }
    return request_name_result;
}

//获取硬盘序列号
QString get_harddisk_id()
{
    int ret = 0;
    gchar *request_name_result = NULL;
    guint request_name_int =0;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "get_harddisk_id", &error,
                    G_TYPE_INVALID,
                    G_TYPE_STRING, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("get_harddisk_id->%s\n", request_name_result);
        printf("get_harddisk_id->%d\n", request_name_int);
        error = NULL;
    }
    return request_name_result;
}

//获取UUID
QString get_system_uuid()
{
    int ret = 0;
    gchar *request_name_result = NULL;
    guint request_name_int =0;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "get_system_uuid", &error,
                    G_TYPE_INVALID,
                    G_TYPE_STRING, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("get_system_uuid->%s\n", request_name_result);
        printf("get_system_uuid->%d\n", request_name_int);
        error = NULL;
    }
    return request_name_result;
}

//获取网卡Mac
QString get_interface_mac()
{
    int ret = 0;
    gchar *request_name_result = NULL;
    guint request_name_int =0;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "get_interface_mac", &error,
                    G_TYPE_INVALID,
                    G_TYPE_STRING, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("get_interface_mac->%s\n", request_name_result);
        printf("get_interface_mac->%d\n", request_name_int);
        error = NULL;
    }
    return request_name_result;
}

//获取客户单位
QString get_customer()
{
    int ret = 0;
    gchar *request_name_result = NULL;
    guint request_name_int =0;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "get_customer", &error,
                    G_TYPE_INVALID,
                    G_TYPE_STRING, &request_name_result,
                    G_TYPE_INT, &request_name_int,
                    G_TYPE_INVALID);
        printf("get_customer->%s\n", request_name_result);
        printf("get_customer_i->%d\n", request_name_int);
        error = NULL;
    }
    return request_name_result;
}
//设置客户单位
int set_customer(gchar *code)
{
    int ret = 0;
    gint request_name_result = 0;
    if (proxy != NULL) {
        ret = dbus_g_proxy_call(proxy, "set_customer", &error,
                    G_TYPE_STRING, code,
                    G_TYPE_INVALID,
                    G_TYPE_INT, &request_name_result,
                    G_TYPE_INVALID);
        printf("set_customer->%d\n", request_name_result);
        error = NULL;
    }
    return request_name_result;
}

bool import_auth_file(gchar *path)
{
    bool ret=false;
    gboolean request_name_result=false;
    if(proxy!=NULL)
    {
        ret=dbus_g_proxy_call(proxy, "import_auth_file", &error,
                              G_TYPE_STRING, path,
                              G_TYPE_INVALID,
                              G_TYPE_BOOLEAN, &request_name_result,
                              G_TYPE_INVALID);
        error=NULL;
    }
    return request_name_result;
}

bool remove_activation_file()
{
    bool ret=false;
    gboolean request_name_result=false;
    if(proxy!=NULL)
    {
        ret=dbus_g_proxy_call(proxy, "remove_activation_file", &error,
                              G_TYPE_INVALID,
                              G_TYPE_BOOLEAN, &request_name_result,
                              G_TYPE_INVALID);
        error=NULL;
    }
    return request_name_result;
}

bool remove_license_file()
{
    bool ret=false;
    gboolean request_name_result=false;
    if(proxy!=NULL)
    {
        ret=dbus_g_proxy_call(proxy, "remove_license_file", &error,
                              G_TYPE_INVALID,
                              G_TYPE_BOOLEAN, &request_name_result,
                              G_TYPE_INVALID);
        error=NULL;
    }
    return request_name_result;
}

QString get_service_tag()
{
    bool ret=false;
    gchar *request_name_result = NULL;
    if(proxy!=NULL)
    {
        ret=dbus_g_proxy_call(proxy, "get_service_tag", &error,
                              G_TYPE_INVALID,
                              G_TYPE_STRING, &request_name_result,
                              G_TYPE_INVALID);
        error=NULL;
    }
    return QString::fromLocal8Bit(request_name_result);
}

QString get_vendor()
{
    bool ret=false;
    gchar *request_name_result = NULL;
    if(proxy!=NULL)
    {
        ret=dbus_g_proxy_call(proxy, "get_vendor", &error,
                              G_TYPE_INVALID,
                              G_TYPE_STRING, &request_name_result,
                              G_TYPE_INVALID);
        error=NULL;
    }
    return QString::fromLocal8Bit(request_name_result);
}

QString get_model()
{
    bool ret=false;
    gchar *request_name_result = NULL;
    if(proxy!=NULL)
    {
        ret=dbus_g_proxy_call(proxy, "get_model", &error,
                              G_TYPE_INVALID,
                              G_TYPE_STRING, &request_name_result,
                              G_TYPE_INVALID);
        error=NULL;
    }
    return QString::fromLocal8Bit(request_name_result);
}

bool check_oem()
{
    bool ret=false;
    gboolean request_name_result=false;
    if(proxy!=NULL)
    {
        ret=dbus_g_proxy_call(proxy, "check_oem", &error,
                              G_TYPE_INVALID,
                              G_TYPE_BOOLEAN, &request_name_result,
                              G_TYPE_INVALID);
        error=NULL;
    }
    return request_name_result;
}

int license_check(gchar *l_file, gchar *i_file)
{
    bool ret=false;
    gint request_name_result;
    if(proxy!=NULL)
    {
        ret=dbus_g_proxy_call(proxy, "license_check", &error,
                              G_TYPE_STRING, l_file,
                              G_TYPE_STRING, i_file,
                              G_TYPE_INVALID,
                              G_TYPE_INT, &request_name_result,
                              G_TYPE_INVALID);
        error=NULL;
    }
    return request_name_result;
}
